package bird.stack.linkedlist_stack;

import bird.linkedlist.LinkedList;
import bird.stack.Stack;

/**
 * @author sijunqiang
 * @data 2021/5/24 22:54
 * @description: 基于链表实现的栈
 * 栈的特点是 先进后出 FILO
 */
public class LinkedListStack<E> implements Stack<E> {


    private LinkedList<E> linkedList;

    public LinkedListStack() {
        linkedList = new LinkedList<>();
    }

    @Override
    public void push(E e) {
        // 入栈 -就是往链表头添加元素
        linkedList.addFirst(e);
    }

    @Override
    public E pop() {
        // 出栈-就是取出链表头的元素 FILO
        return linkedList.removeFirst();
    }

    @Override
    public E peek() {
        // 查看栈顶的元素
        return linkedList.getFirst();
    }

    @Override
    public int getSize() {
        return linkedList.getSize();
    }

    @Override
    public Boolean isEmpty() {
        return linkedList.isEmpty();
    }

    @Override
    public String toString() {
        StringBuilder res = new StringBuilder();
        res.append("stack: top");
        res.append(linkedList);
        return res.toString();
    }
}
